package com.example.clxy.service.impl;

import com.example.clxy.dao.ScheduleMapper;
import com.example.clxy.dao.SemesterMapper;
import com.example.clxy.entity.Semester;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.SchedulePageResult;
import com.example.clxy.result.business.PageResult.SemesterPageResult;
import com.example.clxy.result.business.SemesterResult;
import com.example.clxy.result.business.SemesterResult2;
import com.example.clxy.service.SemesterService;
import com.example.clxy.util.Tools;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liuhaoqi
 * @Date 2020/8/3 21:52
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class SemesterServiceImpl implements SemesterService {

    @Autowired
    SemesterMapper semesterMapper;
    @Autowired
    ScheduleMapper scheduleMapper;

    /**
     * 获取所有学期信息
     * @param semestername
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<SemesterPageResult> getSemesterList(String semestername, int page, int pagesize) {
        PageResult<SemesterPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<SemesterPageResult> list = semesterMapper.getSemesterList(semestername, begin, end, pagesize);
        List<SchedulePageResult> semesterProgramList = scheduleMapper.getSemesterProgram(0, 10000, 10000, null, null, null);
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < semesterProgramList.size(); j++) {
                if (list.get(i).getSemesterid().equals(semesterProgramList.get(j).getSemesterid())) {
                    list.get(i).setStatus("1");
                }
            }
            if (!"1".equals(list.get(i).getStatus()) || list.get(i).getStatus() == null || "".equals(list.get(i).getStatus())) {
                list.get(i).setStatus("0");
            }
        }
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取所有学期信息成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取所有学期信息失败");
        }
        return result;
    }

    /**
     * 新增学期
     * @param semester
     * @return
     */
    @Override
    public WebRestResult addSemester(Semester semester) {
        WebRestResult result = new WebRestResult();
        semester.setSemesterid(UUIDUtil.randomUUID());
        semester.setFlag("0");
        semester.setCreatedTime(new Date());
        semester.setUpdatedTime(new Date());
        if (semesterMapper.insert(semester) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增学期成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增学期失败");
        }
        return result;
    }

    /**
     * 修改学期
     * @param semester
     * @return
     */
    @Override
    public WebRestResult updateSemester(Semester semester) {
        WebRestResult result = new WebRestResult();
        semester.setUpdatedTime(new Date());
        if (semesterMapper.updateByPrimaryKeySelective(semester) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("修改学期信息成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改学期信息失败");
        }
        return result;
    }

    /**
     * 删除学期
     * @param semesterid
     * @return
     */
    @Override
    public WebRestResult deleteSemester(String semesterid) {
        WebRestResult result = new WebRestResult();
        if (semesterMapper.deleteSemester(semesterid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除学期成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除学期失败");
        }
        return result;
    }

    @Override
    public SemesterResult getSemesterById(String semesterid) {
        SemesterResult result = new SemesterResult();
        SemesterResult semester = semesterMapper.getSemesterById(semesterid);
        if (semester != null) {
            BeanUtils.copyProperties(semester, result);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取单个学期信息成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取单个学期信息失败！");
        }
        return result;
    }

    @Override
    public SemesterResult getSemesterByDate(String date) {
        SemesterResult result = semesterMapper.getSemesterByDate(date);
        if (result != null) {
            result.setMessage("根据当前日期获取最近学期成功！");
            result.setResult(WebRestResult.SUCCESS);
            String date2Str = Tools.date2Str(result.getStartdate(), "yyyy-MM-dd");
            Date date1 = Tools.str2Date1(date2Str);
            result.setStartdate(date1);
        } else {
            result.setMessage("根据当前日期获取最近学期失败！");
            result.setResult(WebRestResult.FAILURE);
        }
        return result;
    }

    @Override
    public SemesterResult2 getSemesterById2(String semesterid) {
        Semester semester = semesterMapper.selectByPrimaryKey(semesterid);
        SemesterResult2 result = new SemesterResult2();
        if (semester != null) {
            BeanUtils.copyProperties(semester, result);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取单个学期信息成功！");
        }
        return result;
    }


}
